package com.edge.pbx.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.edge.pbxdsp.R;


public class PbxLoadingDialog extends Dialog {

    private ImageView ivprogress;
    private TextView txtbluetooth;
    private Animation animation;

    public PbxLoadingDialog(Context context) {
        super(context, com.qztech.sdk.R.style.LoadingDialog);
        initView();
    }

    public PbxLoadingDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        initView();
    }

    private void initView() {
//        WindowManager wm = (WindowManager) getContext().getSystemService( Context.WINDOW_SERVICE);
//        DisplayMetrics outMetrics = new DisplayMetrics();
//        wm.getDefaultDisplay().getMetrics(outMetrics);
//        WindowManager.LayoutParams attributes = getWindow().getAttributes();
//        attributes.height = outMetrics.heightPixels;
//        attributes.width = outMetrics.widthPixels;
//        getWindow().setAttributes(attributes);
        setContentView(getLayoutInflater().inflate(R.layout.dialog_loading, null), new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
//        setContentView(R.layout.dialog_bluetooth);
        if (getWindow() != null) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }
        this.txtbluetooth = (TextView) findViewById(R.id.txt_bluetooth);
        this.ivprogress = (ImageView) findViewById(R.id.iv_progress);
        animation = AnimationUtils.loadAnimation(getContext(), com.qztech.sdk.R.anim.loading_bluetooth);
    }


    public void showLoading() {
        ivprogress.setBackgroundResource(R.drawable.pbx_loading_circle);
        View view = findViewById(R.id.pbx_loading_logo);
        if (view != null)
            view.setVisibility(View.VISIBLE);
//        ivprogress.setImageResource(R.drawable.loading);
        if (ivprogress != null){
            ivprogress.clearAnimation();
            RotateAnimation animation = new RotateAnimation(0, 36000, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
//            Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.loading_turn);
            animation.setDuration(500000);
            animation.setInterpolator(new LinearInterpolator());
            animation.setRepeatCount(Animation.INFINITE);
            ivprogress.startAnimation(animation);
        }

        show();
    }

    public void showSuccess(String errorMsg) {
        View view = findViewById(R.id.pbx_loading_logo);
        if (view != null)
            view.setVisibility(View.GONE);
        setMessage(errorMsg, Color.WHITE);
        ivprogress.setAnimation(null);
        ivprogress.setBackgroundResource(com.hanlink.res.R.drawable.bluecon_1);
        if (!isShowing()) {
            show();
        }
        ivprogress.postDelayed(() -> {
            dismiss();
        }, 2000);
    }

    public void showFail(String errorMsg) {
        View view = findViewById(R.id.pbx_loading_logo);
        if (view != null)
            view.setVisibility(View.GONE);
        setMessage(errorMsg,Color.WHITE);
        ivprogress.setAnimation(null);
        ivprogress.setBackgroundResource(com.hanlink.res.R.drawable.bluecon_0);
        if (!isShowing()) {
            show();
        }
        ivprogress.postDelayed(() -> {
            dismiss();
        }, 2000);
    }

    public void setMessage(String msg, int color) {
        txtbluetooth.setTextColor(color);
        txtbluetooth.setText(msg);
    }

    public void setMessage(int resId) {
        txtbluetooth.setText(getContext().getString(resId));
    }

    @Override
    public void show() {
        super.show();
    }

    @Override
    public void dismiss() {
        if (ivprogress != null) {
            ivprogress.clearAnimation();
//            ivprogress.setAnimation(null);
        }
        super.dismiss();
    }
}
